import java.util.Arrays;

public class Heap {
    int []elem;
    int uS;
    public  Heap(int []elem){
        this.elem=new int[10];
    }
    public void initHeap(int []array){
        if(isFull()){
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        for (int i = 0; i < array.length; i++) {
            this.elem[i]=array[i];
            this.uS++;
        }
    }
    public boolean isFull(){
        return elem.length==this.uS;
    }
    public  void createHeap(){
        int child=this.uS;
        for (int parent =(child-1-1)/2; parent>=0; parent--) {
            sitDown(parent,this.uS);
        }
    }
    public void sitDown(int parent,int uS){
        int child=parent*2+1;
        while(child<uS){
            if(child+1<uS&&elem[child]<elem[child+1]){
                child++;
            }
            if(elem[parent]<elem[child]){
                swap(elem,parent,child);
                parent=child;
                child=2*parent+1;
            }else {
                break;
            }
        }
    }
    public void swap(int []elem,int parent,int child){
        int tmp=elem[parent];
        elem[parent]=elem[child];
        elem[child]=tmp;
    }
    public void offer(int a){
        if(isFull()){
            elem= Arrays.copyOf(elem,2*elem.length);
        }
        elem[uS]=a;
        sitUp(uS);
        uS++;
    }
    public void sitUp(int child){
      int parent=(child-1)/2;
      while (parent>=0){
          if(elem[child]>elem[parent]){
              swap(elem,parent,child);
              child=parent;
              parent=(child-1)/2;
          }else {
              break;
          }
      }
    }
    public int poll(){
        if(isEmpty()){
            return -1;
        }
        int val=elem[0];
        swap(elem,0,uS-1);
        sitDown(0,uS);
        uS--;
        return val;
    }
    public boolean isEmpty(){
        return uS==0;
    }
    public int peek(){
        if(isEmpty()){
            return -1;
        }
        return elem[0];
    }

}
